﻿DELIMITER //

--
--
DROP PROCEDURE IF EXISTS spZeusPickingAdHoc
//

-- HU - Rakomány info
-- EN - Package information
--
--
CREATE PROCEDURE spZeusPickingAdHoc 
  (_AccessToken INTEGER, _PickingID INTEGER, _Code VARCHAR(20), _Quantity DECIMAL(18,6))
BEGIN
  DECLARE _Rows INTEGER;
  DECLARE _UserID INTEGER;
  DECLARE _Lang VARCHAR(10);
  DECLARE _Status VARCHAR(1);
  DECLARE _ErrorText VARCHAR(128);

  --
  --
  CALL spAccessTokenCheck(_AccessToken, _UserID, _Lang);

  
  IF (IFNULL(_Quantity, 0) <= 0) THEN

    IF (_Lang = 'HU') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Mennyiség nem lehet 0 vagy negatív !';
    END IF;

    IF (_Lang = 'EN') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Quantity could not been equal or less than Zero !';
    END IF;

    IF (_Lang = 'DE') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Menge kann nicht negativ sein !';
    END IF;
  END IF;


  BEGIN
    DECLARE EXIT HANDLER FOR NOT FOUND SET _Rows = 0;

    SELECT Status
      INTO _Status
      FROM ZeusPicking
     WHERE ID = _PickingID;

    SET _Rows = 1;
  END;

  --
  --
  IF (_Rows != 1) OR (_Status != 'O') THEN

    IF (_Lang = 'HU') THEN
      SELECT _ErrorText = REPLACE(N'Nincs ilyen Kiszedés ID %s !', '%s' , _PickingID) ;
    END IF;

    IF (_Lang = 'EN') THEN
      SELECT _ErrorText = REPLACE(N'There is no such picking ID %s !', '%s' , _PickingID);
    END IF;

    IF (_Lang = 'DE') THEN
      SELECT _ErrorText = REPLACE(N'gibt es keine solche Kommissionierung ID %s !', '%s' , _PickingID);
    END IF;

    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = _ErrorText;

  END IF;

  BEGIN
    DECLARE EXIT HANDLER FOR SQLSTATE '23000' SET _Rows = 0;
        
    INSERT INTO ZeusPickingItem
      (PickingID, Code, ArticleCode, ArticleName, Expired, LocationCode, Quantity, UnitCode, PickedQua)
    SELECT _PickingID, Code, ArticleCode, ArticleName, Expired, LocationCode, _Quantity, UnitCode, _Quantity
      FROM ZeusPackage
     WHERE Code = _Code;
         
    SET _Rows = 1;
  END;


  --
  --
  IF (_Rows != 1) THEN

    IF (_Lang = 'HU') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Kiszedési tétel mentése nem sikerült !';
    END IF;

    IF (_Lang = 'EN') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Failed to save picking item !';
    END IF;

    IF (_Lang = 'DE') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Sparen Kommissionierung Artikel fehlgeschlagen !';
    END IF;

  END IF;


  -- HU - Visszaadja ennek a Rakománynak az adatait
  -- EN - Get the package details
  --
  SELECT Code, ArticleCode, ArticleName, CAST(Expired as DATE) Expired, LocationCode, 

         _Quantity Quantity, UnitCode, '' Notes

    FROM ZeusPackage
   WHERE Code = _Code;
END
//
